//
//  HomeFuntionCell.m
//  CarUser
//
//  Created by 王森林 on 2018/12/4.
//  Copyright © 2018年 王森林. All rights reserved.
//

#import "HomeFuntionCell.h"
#import "HomeArticleDetailViewController.h"
#import "HomeSendViewController.h"
#import "NewsMaintainViewController.h"
#import "NewsInsureanceViewController.h"
@interface HomeFuntionCell()
@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) NSArray *functionAry;
@end

#define BTNTAG 10000000
@implementation HomeFuntionCell

- (void)createUIWithData
{
    self.backgroundColor = [UIColor clearColor];
    [self.bgView removeAllSubviews];
    [self.contentView addSubview:self.bgView];
    [self createConstraint];
    
    [self.functionAry enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        NSString *imageName  =   [obj  objectForKey:@"imageName"];
        NSString *title = [obj objectForKey:@"title"];
        [self createBtnWithImg:imageName withTitle:title withIndex:idx];
        
    }];
    
}

- (void)createConstraint
{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.contentView.mas_top).offset(10);
        (void)make.left.and.right;
        make.bottom.equalTo(self.contentView.mas_bottom).offset(-10);
    }];
    
    
}

- (void)setData:(id)data
{
    if (data){
        self.functionAry = [NSArray arrayWithArray:data];
        [self createUIWithData];
    }
}

- (void)handleBtnAction:(id)sender
{
    UIButton *btn = sender;
    NSInteger tag  = btn.tag - BTNTAG;
    switch (tag) {
        case 0:
        {
            //港口直销
            if (self.pushType == 1) {
                
                NewsMaintainViewController *controller = [[NewsMaintainViewController alloc] init];
                controller.hidesBottomBarWhenPushed = YES;
                [[self viewController].navigationController  pushViewController:controller animated:YES];

//                HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
//                controller.article_id = @"eaf01660e96b46f18c8300e1d5f1e81e";
//                controller.title = @"港口直销详情";
//                [[self viewController].navigationController pushViewController:controller animated:YES];
            } else {
                HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
                controller.article_id = @"b7a828e9f8cf4b0bba72b2eae97185fe";
                controller.title = @"车辆维修详情";
                [[self viewController].navigationController pushViewController:controller animated:YES];
                
            }
         
            
        }
            break;
        case 1:
        {
            //质量三包
            if (self.pushType == 1) {
                NewsInsureanceViewController *controller = [[NewsInsureanceViewController alloc] init];
                controller.hidesBottomBarWhenPushed = YES;
                [[self viewController].navigationController  pushViewController:controller animated:YES];

//                HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
//                controller.article_id = @"ddf57f6ebb50425c94330a96d089f4a1";
//                controller.title = @"质量三包详情";
//                [[self viewController].navigationController pushViewController:controller animated:YES];
            } else {
                
                HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
                controller.article_id = @"3ac13511722e4a45be52ba605265984d";
                controller.title = @"车辆保养详情";
                [[self viewController].navigationController pushViewController:controller animated:YES];
            }
          
        }
            break;
        case 2:
        {
            //配送到家
            if (self.pushType == 1) {
                                HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
                                controller.article_id = @"ddf57f6ebb50425c94330a96d089f4a1";
                                controller.title = @"质量三包详情";
                                [[self viewController].navigationController pushViewController:controller animated:YES];

//                HomeSendViewController *controller = [[HomeSendViewController alloc] init];
//                controller.hidesBottomBarWhenPushed = YES;
//                [[self viewController].navigationController pushViewController:controller animated:YES];
            } else {
                HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
                controller.article_id = @"19c542ac8eb7477f88eec29bb50d640f";
                controller.title = @"保险服务详情";
                [[self viewController].navigationController pushViewController:controller animated:YES];
                
            }

        }
            break;
        case 3:
        {
            //线下体验
            if (self.pushType == 1) {
                HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
                controller.article_id = @"11c07ad2abd3449489f648356a01dda0";
                controller.title = @"线下体验详情";
                [[self viewController].navigationController pushViewController:controller animated:YES];
            } else {
                
                HomeArticleDetailViewController *controller  = [[HomeArticleDetailViewController alloc] init];
                controller.article_id = @"f5f0df90a2294c17941f503a74056fc3";
                controller.title = @"金融服务详情";
                [[self viewController].navigationController pushViewController:controller animated:YES];
            }
           
        }
            break;
            
        default:
            break;
    }
    
}


-  (void )createBtnWithImg:(NSString *)imgName withTitle:(NSString *)title withIndex:(NSInteger)index
{
    UIImage *image = IMG(imgName);
//    float marX = 40;
//    float  width  = (BOUNDWIDTH  - IMGWIDTH(image)*4-marX*2)/3;
    float width = BOUNDWIDTH/4;
    float  height  = 105;
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame =  CGRectMake(width*index, 20, width, height);
    btn.tag = BTNTAG + index;
    [btn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];

    
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake((width-50)/2, 0, 50, 50)];
    imageView.image = image;
    [btn addSubview:imageView];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 60, width, 15)];
    titleLabel.text = title;
    titleLabel.font = FONT_SIZE(12);
    titleLabel.textAlignment  =  NSTextAlignmentCenter;
    titleLabel.textColor = RGB(49, 49, 49);
    [btn addSubview:titleLabel];
    
//    [btn setTitle:title forState:UIControlStateNormal];
//    [btn setImage:image forState:UIControlStateNormal];
//    [btn setTitleColor:Common_TextColor forState:UIControlStateNormal];
//    btn.titleLabel.font = FONT_SIZE(12);
//    btn.tag = BTNTAG + index;
//    [btn addTarget:self action:@selector(handleBtnAction:) forControlEvents:UIControlEventTouchUpInside];
//    btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;//使图片和文字水平居中显示
////    UIEdgeInsetsMake(<#CGFloat top#>, <#CGFloat left#>, <#CGFloat bottom#>, <#CGFloat right#>)
//    [btn setTitleEdgeInsets:UIEdgeInsetsMake(btn.imageView.frame.size.height-20 ,-btn.imageView.frame.size.width, 0.0,0.0)];//文字距离上边框的距离增加imageView的高度，距离左边框减少imageView的宽度，距离下边框和右边框距离不变
//    [btn setImageEdgeInsets:UIEdgeInsetsMake( -btn.titleLabel.bounds.size.height-40-8, 0.0,0.0, -btn.titleLabel.bounds.size.width)];//图片距离右边框距离减少图片的宽度，其它不边
    
    [self.bgView addSubview:btn];
}


-  (UIView *)bgView
{
    if (!_bgView) {
        _bgView = [UIView new];
        _bgView.backgroundColor = [UIColor whiteColor];
    }
    return _bgView;
    
}

@end
